﻿<UserControl x:Class="Waf.MusicManager.Presentation.Views.ManagerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:p="clr-namespace:Waf.MusicManager.Presentation.Properties"
             xmlns:ctrl="clr-namespace:Waf.MusicManager.Presentation.Controls"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:dd="clr-namespace:Waf.MusicManager.Presentation.DesignData"
             xmlns:domain="clr-namespace:Waf.MusicManager.Domain.MusicFiles;assembly=Waf.MusicManager.Domain"
             mc:Ignorable="d" d:DataContext="{d:DesignInstance dd:SampleManagerViewModel, IsDesignTimeCreatable=True}"
             d:DesignHeight="300" d:DesignWidth="600" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Button x:Name="directoryButton" Click="DirectoryButtonClick" Height="23" HorizontalContentAlignment="Stretch"
                BorderThickness="0" Padding="5,0" Margin="0,0,1,0" Foreground="White" Background="{StaticResource DarkBackground}">
            <ctrl:PathLabel Path="{Binding FolderBrowser.UserPath}" Padding="0" Foreground="White"/>
        </Button>
        <ctrl:Flyout x:Name="folderBrowserPopup" PlacementTarget="{Binding ElementName=directoryButton}" Placement="Bottom" VerticalOffset="-23"
                     Closed="FolderBrowserPopupClosed">
            <Border BorderBrush="{StaticResource DefaultBorderBrush}" BorderThickness="0,0,0,1" Background="{StaticResource DarkTransparentBackground}">
                <Grid Height="135" Margin="0,0,0,10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="5"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="10"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <TextBox x:Name="userPathBox" Text="{Binding FolderBrowser.UserPath, ValidatesOnExceptions=true}" KeyDown="UserPathBoxKeyDown" Grid.ColumnSpan="4" 
                             Height="23" BorderThickness="0" VerticalContentAlignment="Center" Padding="3,0" Style="{StaticResource DarkTextBoxStyle}"/>

                    <Border BorderBrush="{StaticResource DefaultBorderBrush}" BorderThickness="0,1,0,0" Grid.ColumnSpan="4" Grid.Row="1"/>

                    <Button Command="{Binding NavigateHomeCommand}" Content="&#xE10F;" ToolTip="{x:Static p:Resources.NavigateToMusicFolder}"
                            Style="{StaticResource IconButtonStyle}" Grid.Row="2" Margin="5,0"/>

                    <Button Command="{Binding NavigateDirectoryUpCommand}" Content="&#xE110;" ToolTip="{x:Static p:Resources.NavigateFolderUp}"
                            Style="{StaticResource IconButtonStyle}" Grid.Column="1" Grid.Row="2" Margin="5,0"/>

                    <Button Command="{Binding NavigatePublicHomeCommand}" Content="&#xE125;" ToolTip="{x:Static p:Resources.NavigateToPublicMusicFolder}"
                            VerticalAlignment="Top" Style="{StaticResource IconButtonStyle}" Grid.Row="4" Margin="5,0"/>

                    <Button Command="{Binding LoadRecursiveCommand}" Click="LoadRecursiveClick" Content="&#xE12B;" ToolTip="{x:Static p:Resources.SearchMusicFilesRecursive}"
                            VerticalAlignment="Top" Style="{StaticResource IconButtonStyle}" Grid.Column="1" Grid.Row="4" Margin="5,0"/>

                    <ListBox ItemsSource="{Binding FolderBrowser.SubDirectories}" SelectedItem="{Binding FolderBrowser.SelectedSubDirectory}" DisplayMemberPath="DisplayName" Grid.Column="2" Grid.Row="2" Grid.RowSpan="3"
                             ScrollViewer.VerticalScrollBarVisibility="Disabled">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel IsItemsHost="True" Orientation="Vertical"/>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                                <Setter Property="MinWidth" Value="100"/>
                                <Setter Property="Padding" Value="4,3,8,3"/>
                                <EventSetter Event="MouseDoubleClick" Handler="DirectoriesListBoxItemMouseDoubleClick"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>
                </Grid>
            </Border>
        </ctrl:Flyout>

        <Button x:Name="searchButton" Content="{Binding SearchFilter.DisplaySearchFilter}" Click="SearchButtonClick" Height="23" Width="175" Grid.Column="1"
                BorderThickness="0" HorizontalContentAlignment="Left" Padding="5,0" Margin="0" Foreground="White" Background="{StaticResource DarkBackground}"/>
        <Button x:Name="clearSearchButton" Command="{Binding ClearSearchCommand}" Background="{StaticResource DarkBackground}" Style="{StaticResource CloseButtonStyle}" Grid.Column="2"/>
        <ctrl:Flyout x:Name="searchPopup" PlacementTarget="{Binding ElementName=clearSearchButton}" Placement="Bottom" HorizontalFlyoutAlignment="Right" VerticalOffset="-23">
            <Border BorderBrush="White" BorderThickness="0,0,0,1" Background="{StaticResource DarkBackground}">
                <Border.Resources>
                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                        <Setter Property="Foreground" Value="White"/>
                    </Style>
                </Border.Resources>
                
                <Grid Margin="0,0,0,10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{StaticResource Layout.LargeColumnSpace}"/>
                        <ColumnDefinition Width="56"/>
                        <ColumnDefinition Width="{StaticResource Layout.ColumnSpace}"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="{StaticResource Layout.LargeColumnSpace}"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="23"/>
                        <RowDefinition Height="{StaticResource Layout.LargeRowSpace}"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="{StaticResource Layout.RowSpace}"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="{StaticResource Layout.RowSpace}"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="{StaticResource Layout.RowSpace}"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="{StaticResource Layout.LargeRowSpace}"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="{StaticResource Layout.LargeRowSpace}"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Label Content="{x:Static p:Resources.Search}" Grid.Column="1"/>
                    <Grid Grid.Column="3" Grid.ColumnSpan="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        
                        <TextBox x:Name="userSearchFilterBox" Text="{Binding SearchFilter.UserSearchFilter}"
                             BorderThickness="1,0,0,0" VerticalContentAlignment="Center" Padding="3,0" Style="{StaticResource DarkTextBoxStyle}"/>
                        <Button Command="{Binding ClearSearchCommand}" Style="{StaticResource CloseButtonStyle}" Grid.Column="1"/>
                    </Grid>

                    <Border BorderBrush="White" BorderThickness="0,1,0,0" Grid.ColumnSpan="5" Grid.Row="1"/>

                    <Label Content="{x:Static p:Resources.Artist}" Grid.Column="1" Grid.Row="2"/>
                    <TextBox Text="{Binding SearchFilter.ArtistFilter}" Grid.Column="3" Grid.Row="2" Style="{StaticResource DarkTextBoxStyle}"/>

                    <Label Content="{x:Static p:Resources.Title}" Grid.Column="1" Grid.Row="4"/>
                    <TextBox Text="{Binding SearchFilter.TitleFilter}" Grid.Column="3" Grid.Row="4" Style="{StaticResource DarkTextBoxStyle}"/>

                    <Label Content="{x:Static p:Resources.Album}" Grid.Column="1" Grid.Row="6"/>
                    <TextBox Text="{Binding SearchFilter.AlbumFilter}" Grid.Column="3" Grid.Row="6" Style="{StaticResource DarkTextBoxStyle}"/>

                    <Label Content="{x:Static p:Resources.Year}" Grid.Column="1" Grid.Row="8"/>
                    <Grid Grid.Column="3" Grid.Row="8">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="{StaticResource Layout.ColumnSpace}"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="{StaticResource Layout.LargeColumnSpace}"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="{StaticResource Layout.ColumnSpace}"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Label Content="{x:Static p:Resources.From}" Padding="0,4"/>
                        <TextBox Text="{Binding SearchFilter.FromYearFilter, ValidatesOnExceptions=True}" Grid.Column="2" Style="{StaticResource DarkTextBoxStyle}"/>

                        <Label Content="{x:Static p:Resources.To}" Padding="0,4" Grid.Column="4"/>
                        <TextBox Text="{Binding SearchFilter.ToYearFilter, ValidatesOnExceptions=True}" Grid.Column="6" Style="{StaticResource DarkTextBoxStyle}"/>
                    </Grid>

                    <Label Content="{x:Static p:Resources.Genre}" Grid.Column="1" Grid.Row="10"/>
                    <ComboBox Text="{Binding SearchFilter.GenreFilter, Converter={StaticResource StringListToStringConverter}, ConverterParameter=ListSeparator, UpdateSourceTrigger=LostFocus}" 
                            ItemsSource="{Binding Path=(domain:Genres.DefaultValues)}"
                            IsEditable="True" Grid.Column="3" Grid.Row="10"/>

                    <Label Content="{x:Static p:Resources.Rating}" Grid.Column="1" Grid.Row="12"/>
                    <Grid Grid.Column="3" Grid.Row="12">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50"/>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <ComboBox ItemsSource="{Binding Source={StaticResource FilterOperatorValues}}" SelectedItem="{Binding SearchFilter.RatingFilterOperator}" 
                                  FontFamily="Consolas" VerticalContentAlignment="Center">
                            <ComboBox.ItemContainerStyle>
                                <Style TargetType="ComboBoxItem" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                </Style>
                            </ComboBox.ItemContainerStyle>
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Converter={StaticResource FilterOperatorToStringConverter}}" 
                                               ToolTip="{Binding Converter={StaticResource FilterOperatorToStringConverter}, ConverterParameter=Description}"
                                               HorizontalAlignment="Stretch" Margin="0,3"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <ctrl:Rating Value="{Binding SearchFilter.RatingFilter, Converter={StaticResource RatingToStarsConverter}}" Grid.Column="2"/>    
                    </Grid>
                </Grid>
            </Border>
        </ctrl:Flyout>
        
        <DataGrid x:Name="musicFilesGrid" ItemsSource="{Binding SelectionService.MusicFiles}" SelectedItem="{Binding SelectedMusicFile}" BorderBrush="White" BorderThickness="0,1,0,0"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.Row="1" Grid.ColumnSpan="3"
                  ctrl:SelectionBehavior.SyncSelectedItems="{Binding SelectionService.SelectedMusicFiles}">
            <DataGrid.Resources>
                <Style TargetType="ctrl:RatingItem">
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="True"/>
                                <Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="True"/>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="LightGray"/>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.InputBindings>
                <KeyBinding Command="{Binding PlayerService.PlaySelectedCommand}" Key="Return"/>
                <KeyBinding Command="{Binding PlayerService.EnqueueSelectedCommand}" Key="Return" Modifiers="Shift"/>
            </DataGrid.InputBindings>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Command="{Binding PlayerService.PlaySelectedCommand}" Header="{x:Static p:Resources.PlaySelection}" InputGestureText="{x:Static p:Resources.Return}"/>
                                <MenuItem Command="{Binding PlayerService.EnqueueSelectedCommand}" Header="{x:Static p:Resources.EnqueueSelection}" InputGestureText="{x:Static p:Resources.ShiftReturn}"/>
                                <MenuItem Command="{Binding TranscodingService.ConvertToMp3SelectedCommand}" Header="{x:Static p:Resources.ConvertToMP3}"/>
                                <Separator/>
                                <MenuItem Command="{Binding ShowMusicPropertiesCommand}" Header="{x:Static p:Resources.ShowDetails}"/>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                    <EventSetter Event="ContextMenuOpening" Handler="DataGridRowContextMenuOpening"/>
                    <EventSetter Event="MouseDoubleClick" Handler="DataGridRowMouseDoubleClick"/>
                    <EventSetter Event="MouseMove" Handler="DataGridRowMouseMove"/>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="{x:Static p:Resources.Artists}" Binding="{Binding ArtistsString}" IsReadOnly="True" Width="200"
                                    ElementStyle="{StaticResource TextCellElementStyle}"/>
                <DataGridTextColumn x:Name="titleColumn" Header="{x:Static p:Resources.Title}" IsReadOnly="True" Width="*"
                                    ElementStyle="{StaticResource TextCellElementStyle}">
                    <DataGridTextColumn.Binding>
                        <MultiBinding Converter="{StaticResource MusicTitleConverter}">
                            <Binding Path="MusicFile.FileName"/>
                            <Binding Path="MusicFile.Metadata.Artists"/>
                            <Binding Path="MusicFile.Metadata.Title"/>
                        </MultiBinding>
                    </DataGridTextColumn.Binding>
                </DataGridTextColumn>
                <DataGridTemplateColumn x:Name="ratingColumn" Header="{x:Static p:Resources.Rating}" SortMemberPath="MusicFile.Metadata.Rating" Width="110" MaxWidth="110">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ctrl:Rating Value="{Binding MusicFile.Metadata.Rating, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource RatingToStarsConverter}}"
                                Padding="3,2" TickPlacement="TopLeft">
                                <ctrl:Rating.Style>
                                    <Style TargetType="ctrl:Rating">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                        <Style.Triggers>
                                            <MultiDataTrigger>
                                                <MultiDataTrigger.Conditions>
                                                    <Condition Binding="{Binding MusicFile.IsMetadataLoaded}" Value="True"/>
                                                    <Condition Binding="{Binding MusicFile.Metadata.IsSupported}" Value="True"/>
                                                </MultiDataTrigger.Conditions>    
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </MultiDataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ctrl:Rating.Style>
                            </ctrl:Rating>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn x:Name="genreColumn" Header="{x:Static p:Resources.Genre}" Binding="{Binding MusicFile.Metadata.Genre, Converter={StaticResource StringListToStringConverter}, ConverterParameter=ListSeparator}" 
                                    IsReadOnly="True" Width="150" ElementStyle="{StaticResource TextCellElementStyle}"/>
                <DataGridTextColumn x:Name="yearColumn" Header="{x:Static p:Resources.Year}" Binding="{Binding MusicFile.Metadata.Year, Converter={StaticResource UIntToDisplayValueConverter}}" IsReadOnly="True" Width="55" MaxWidth="55"
                                    ElementStyle="{StaticResource TextCellElementStyle}"/>
                <DataGridTextColumn x:Name="albumColumn" Header="{x:Static p:Resources.Album}" Binding="{Binding MusicFile.Metadata.Album}" IsReadOnly="True" Width="200" 
                                    ElementStyle="{StaticResource TextCellElementStyle}"/>
                <DataGridTextColumn x:Name="trackNoColumn" Header="{x:Static p:Resources.TrackNo}" Binding="{Binding MusicFile.Metadata.TrackNumber, Converter={StaticResource UIntToDisplayValueConverter}}" 
                                    IsReadOnly="True" Width="SizeToHeader" MinWidth="25">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextCellElementStyle}">
                            <Setter Property="HorizontalAlignment" Value="Right"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="{x:Static p:Resources.Length}" Binding="{Binding MusicFile.Metadata.Duration, Converter={StaticResource DurationConverter}}" 
                                    IsReadOnly="True" Width="SizeToHeader" CanUserResize="False">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextCellElementStyle}">
                            <Setter Property="HorizontalAlignment" Value="Right"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

        
        <Button x:Name="statusBarButton" Click="StatusBarButtonClick" Height="21" Grid.Row="2" Grid.ColumnSpan="3" Background="{StaticResource DarkBackground}" BorderThickness="0" HorizontalContentAlignment="Stretch" Padding="0">
            <Grid Margin="7,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                
                <TextBlock Grid.Column="0">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="Text" Value="{Binding ManagerStatusService.TotalFilesCount, Converter={StaticResource ItemsCountConverter}}"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ManagerStatusService.TotalFilesCount}" Value="-1">
                                    <Setter Property="Text" Value="{x:Static p:Resources.Updating}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>

                <TextBlock Text="&#xE10C;" FontFamily="Segoe UI Symbol" FontSize="10" VerticalAlignment="Center" Grid.Column="1"/>
            </Grid>
        </Button>

        <ProgressBar IsIndeterminate="{Binding ManagerStatusService.UpdatingFilesList}" 
            Grid.Row="2" Grid.ColumnSpan="3" VerticalAlignment="Top">
            <ProgressBar.Style>
                <Style TargetType="ProgressBar" BasedOn="{StaticResource {x:Type ProgressBar}}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ManagerStatusService.UpdatingFilesList}" Value="False">
                            <Setter Property="Visibility" Value="Hidden"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ProgressBar.Style>
        </ProgressBar>

        <ctrl:Flyout x:Name="menuPopup" PlacementTarget="{Binding ElementName=statusBarButton}" Placement="Top">
            <Border BorderBrush="White" BorderThickness="0,1" Background="{StaticResource DarkBackground}">
                <Grid Margin="5,10,5,5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="80"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Button Command="{Binding PlayerService.PlayAllCommand}" Content="&#xE29B;" Style="{StaticResource IconButtonStyle}" Grid.Row="0" Grid.Column="0"/>
                    <TextBlock Text="{x:Static p:Resources.PlayAll}" Style="{StaticResource AppBarTextBlockStyle}" Grid.Row="1" Grid.Column="0"/>

                    <Button Command="{Binding PlayerService.EnqueueAllCommand}" Content="&#xE154;" Style="{StaticResource IconButtonStyle}" Grid.Row="0" Grid.Column="1"/>
                    <TextBlock Text="{x:Static p:Resources.EnqueueAll}" Style="{StaticResource AppBarTextBlockStyle}" Grid.Row="1" Grid.Column="1"/>

                    <Button Command="{Binding TranscodingService.ConvertToMp3AllCommand}" Content="&#xE118;" Style="{StaticResource IconButtonStyle}" Grid.Row="0" Grid.Column="2"/>
                    <TextBlock Text="{x:Static p:Resources.ConvertToMP3}" Style="{StaticResource AppBarTextBlockStyle}" Grid.Row="1" Grid.Column="2"/>
                </Grid>
            </Border>
        </ctrl:Flyout>
    </Grid>
</UserControl>
